#include "formjf.h"
#include "ui_formjf.h"
#include <mysql.h>
#include <QClipboard>
FormJF::FormJF(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormJF)
{
    ui->setupUi(this);
    this->setWindowTitle("核算经费管理");
    this->show();
    JFSZRefTree(); //刷新经费设置树形结构
}

FormJF::~FormJF()
{
    delete ui;
}
void FormJF::JFSZRefTree()
{
    ui->treeJFSZWidget->clear();
    ui->treeJFSZWidget->setHeaderLabels(QStringList() << "经费编码" << "经费名称"  );
    ui->treeJFSZWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    //ui->treeWidget->setStyleSheet("QTreeWidget::item{border-right: 1px solid blue;border-left: 1px solid blue;border-bottom: 1px solid blue;}"); //加表格线
    QString SQL="";
    QStringList child;
    SQL=QString("SELECT JF_id,JF_name  FROM `JF_set` WHERE type_id='%1' order by JF_id").arg(mySQL::TypeId);
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("JF_id").toString().trimmed();
         child.clear();
         child << query.value("JF_id").toString();
         child << query.value("JF_name").toString();

         QTreeWidgetItem * citem =new QTreeWidgetItem(child);
         if (str.length() ==1){
             ui->treeJFSZWidget->addTopLevelItem(citem);
         }else{
             //遍历找出父节点
             bool f=false;
             QTreeWidgetItemIterator it(ui->treeJFSZWidget);
                 while (*it) {
                      //(*it)->text(0);
                     if ((*it)->text(0)==str.left(str.length()-1)){
                         (*it)->addChild(citem);
                         f=true;
                     }
                     ++it;
                 }
                 if(f==false){
                     //如果减1个字符没照到上级 就减2个字符
                     QTreeWidgetItemIterator it(ui->treeJFSZWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-2)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
                 if(f==false){
                     //如果减2个字符没照到上级 就减3个字符
                     QTreeWidgetItemIterator it(ui->treeJFSZWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-3)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
         }
    }
}

void FormJF::on_btnJFSZSave_clicked()
{
    QString SQL="";
    QString JF_ID=ui->txtJFID->text().trimmed();
    QString JF_NAME=ui->txtJFMC->text().trimmed();
    QString JF_KMDM=ui->txtKMDM->text().trimmed();
    QString JF_KMMC=ui->txtKMMC->text().trimmed();
    QString JF_XMBH=ui->txtXMBH->text().trimmed();
    QString JF_KTBH=ui->txtKTBH->text().trimmed();
    QString JF_KTMC=ui->txtKTMC->text().trimmed();
    QString JF_BZ=ui->txtBZ->text().trimmed();
    QString JF_FZR=ui->txtFZR->text().trimmed();
    QString Idd="";
    bool Ssign=false;
    bool Tsign=false;
    if (JF_ID==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT idd FROM JF_set WHERE JF_id ='%1' and type_id='%2' ").arg(JF_ID.trimmed()).arg(mySQL::TypeId);
    query.exec(SQL);
    Ssign=false;
    while(query.next()){
        Ssign=true;
        Idd=query.value("idd").toString();
    }
    Tsign=true;
    if (Ssign==false && JF_ID.length()>1){
        //新建 并 科目代码长度小于1  就判断是否存在上级科目
        int kl=JF_ID.length()-1;
        SQL=QString("SELECT idd FROM JF_set WHERE  type_id='%2' and (JF_id ='%1' ").arg(JF_ID.left(kl).trimmed()).arg(mySQL::TypeId);
        if (kl>=2){
            //兼容2位科目代码
            kl--;
            SQL+=QString(" or JF_id='%1'").arg(JF_ID.left(kl));
        }
        SQL+= ")";
        query.exec(SQL);
        Tsign=false;
        while(query.next()){
            Tsign=true;
        }
    }
    if (Tsign==true){
        //开始准备保存科目代码
        SQL="";
        if (Ssign==false ){
            SQL="INSERT INTO  JF_set(type_id,JF_id,JF_name,kmdm,kmmc,xmbh,ktbh,ktmc,fzr,bz) VALUES (";
            SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_ID.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_NAME.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KMDM.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KMMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_XMBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KTBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KTMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_FZR.trimmed().replace("'","''"));
            SQL+=QString("'%1')").arg(JF_BZ.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }else{
            SQL="update JF_set set ";
            SQL+=QString("type_id='%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("JF_id='%1',").arg(JF_ID.trimmed().replace("'","''"));
            SQL+=QString("JF_name='%1',").arg(JF_NAME.trimmed().replace("'","''"));
            SQL+=QString("kmdm='%1',").arg(JF_KMDM.trimmed().replace("'","''"));
            SQL+=QString("kmmc='%1',").arg(JF_KMMC.trimmed().replace("'","''"));
            SQL+=QString("xmbh='%1',").arg(JF_XMBH.trimmed().replace("'","''"));
            SQL+=QString("ktbh='%1',").arg(JF_KTBH.trimmed().replace("'","''"));
            SQL+=QString("ktmc='%1',").arg(JF_KTMC.trimmed().replace("'","''"));
            SQL+=QString("fzr='%1',").arg(JF_FZR.trimmed().replace("'","''"));
            SQL+=QString("bz='%1' ").arg(JF_BZ.trimmed().replace("'","''"));
            SQL+=QString("where idd=%1").arg(Idd.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }
        if (SQL!=""){
            query.exec(SQL);
            QMessageBox::information(this,"信息","保存成功！");
            JFSZRefTree();
        }

    }else{
        QMessageBox::warning(this,"警告","不存在上级项目代码 无法保存！！");
    }
}

void FormJF::on_treeJFSZWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    QString SQL="";
    QString ND=mySQL::ND;
    QString JF_id=item->text(0);
    SQL="";
    int i=column;
    i++;
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT type_id,JF_id,JF_name,kmdm,kmmc,xmbh,ktbh,ktmc,fzr,bz  FROM JF_set WHERE JF_id = '%1' and type_id='%2' ").arg(JF_id.trimmed()).arg(mySQL::TypeId);
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    SQL="";
    ui->txtJFID->setText("");
    ui->txtJFMC->setText("");
    ui->txtKMDM->setText("");
    ui->txtKMMC->setText("");
    ui->txtXMBH->setText("");
    ui->txtKTBH->setText("");
    ui->txtKTMC->setText("");
    ui->txtFZR->setText("");
    ui->txtBZ->setText("");
    while(query.next()){
        ui->txtJFID->setText(query.value("JF_id").toString().trimmed());
        ui->txtJFMC->setText(query.value("JF_name").toString().trimmed());
        ui->txtKMDM->setText(query.value("kmdm").toString().trimmed());
        ui->txtKMMC->setText(query.value("kmmc").toString().trimmed());
        ui->txtXMBH->setText(query.value("xmbh").toString().trimmed());
        ui->txtKTBH->setText(query.value("ktbh").toString().trimmed());
        ui->txtKTMC->setText(query.value("ktmc").toString().trimmed());
        ui->txtFZR->setText(query.value("fzr").toString().trimmed());
        ui->txtBZ->setText(query.value("bz").toString().trimmed());
    }
}

void FormJF::on_btnJFSZChild_clicked()
{
    QString SQL="";
    QString JFID="";
    QString Idd="";
    JFID=ui->txtJFID->text().trimmed();
     if (JFID==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT max(JF_id) as t  FROM JF_set WHERE (JF_id like '%1_' or JF_id='%1') and type_id='%2' ").arg(JFID.trimmed()).arg(mySQL::TypeId);

    query.exec(SQL);
    while(query.next()){
        QString str=query.value("t").toString().trimmed();
        if (str==""){
            QMessageBox::warning(this,"警告","未发现项目的存在，请重新选择！");
            return;
        }
        if (str==JFID){
            //如果没有下级科目就添加第一个项目
            JFID+="0";
        }else{
            //有下级科目的情况下取出最大看值
            QString MaxKM=str.right(1).toUtf8();
            int K_ascii ;
            QVector<uint>v=MaxKM.toUcs4();
                foreach(int a,v)
                {
                    qDebug()<<"Ascii:"<<a;
                    K_ascii=a;
                }
                //自动编号的次序为： 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 到Z后将无法编号
                if (K_ascii>=90){
                    QMessageBox::warning(this,"警告","编号已经到了Z了无法再继续了！");
                    return;
                }
                if (K_ascii<57){
                    K_ascii++;
                }else{
                    if (K_ascii==57){
                        K_ascii=65;
                    }else{
                        K_ascii++;
                    }
                }
                JFID+=(QChar)K_ascii;
        }
    }
    QString JF_ID=JFID;
    QString JF_NAME=ui->txtJFMC->text().trimmed();
    QString JF_KMDM=ui->txtKMDM->text().trimmed();
    QString JF_KMMC=ui->txtKMMC->text().trimmed();
    QString JF_XMBH=ui->txtXMBH->text().trimmed();
    QString JF_KTBH=ui->txtKTBH->text().trimmed();
    QString JF_KTMC=ui->txtKTMC->text().trimmed();
    QString JF_BZ=ui->txtBZ->text().trimmed();
    QString JF_FZR=ui->txtFZR->text().trimmed();

    bool Ssign=false;
    bool Tsign=false;
    if (JF_ID==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }

    SQL=QString("SELECT idd FROM JF_set WHERE JF_id ='%1' and type_id='%2' ").arg(JF_ID.trimmed()).arg(mySQL::TypeId);
    query.exec(SQL);
    Ssign=false;
    while(query.next()){
        Ssign=true;
        Idd=query.value("idd").toString();
    }
    Tsign=true;
    if (Ssign==false && JF_ID.length()>1){
        //新建 并 科目代码长度小于1  就判断是否存在上级科目
        int kl=JF_ID.length()-1;
        SQL=QString("SELECT idd FROM JF_set WHERE  type_id='%2' and (JF_id ='%1' ").arg(JF_ID.left(kl).trimmed()).arg(mySQL::TypeId);
        if (kl>=2){
            //兼容2位科目代码
            kl--;
            SQL+=QString(" or JF_id='%1'").arg(JF_ID.left(kl));
        }
        SQL+= ")";
        query.exec(SQL);
        Tsign=false;
        while(query.next()){
            Tsign=true;
        }
    }
    if (Tsign==true){
        //开始准备保存科目代码
        SQL="";
        if (Ssign==false ){
            SQL="INSERT INTO  JF_set(type_id,JF_id,JF_name,kmdm,kmmc,xmbh,ktbh,ktmc,fzr,bz) VALUES (";
            SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_ID.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_NAME.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KMDM.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KMMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_XMBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KTBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KTMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_FZR.trimmed().replace("'","''"));
            SQL+=QString("'%1')").arg(JF_BZ.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }else{
            SQL="update JF_set set ";
            SQL+=QString("type_id='%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("JF_id='%1',").arg(JF_ID.trimmed().replace("'","''"));
            SQL+=QString("JF_name='%1',").arg(JF_NAME.trimmed().replace("'","''"));
            SQL+=QString("kmdm='%1',").arg(JF_KMDM.trimmed().replace("'","''"));
            SQL+=QString("kmmc='%1',").arg(JF_KMMC.trimmed().replace("'","''"));
            SQL+=QString("xmbh='%1',").arg(JF_XMBH.trimmed().replace("'","''"));
            SQL+=QString("ktbh='%1',").arg(JF_KTBH.trimmed().replace("'","''"));
            SQL+=QString("ktmc='%1',").arg(JF_KTMC.trimmed().replace("'","''"));
            SQL+=QString("fzr='%1',").arg(JF_FZR.trimmed().replace("'","''"));
            SQL+=QString("bz='%1' ").arg(JF_BZ.trimmed().replace("'","''"));
            SQL+=QString("where idd=%1").arg(Idd.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }
        if (SQL!=""){
            query.exec(SQL);
            QMessageBox::information(this,"信息","保存成功！");
            JFSZRefTree();
        }

    }else{
        QMessageBox::warning(this,"警告","不存在上级项目代码 无法保存！！");
    }
}

void FormJF::on_btnJFSZJuxtaposition_clicked()
{
    QString SQL="";
    QString JFID="";
    QString Idd="";
    JFID=ui->txtJFID->text().trimmed();
    JFID=JFID.left(JFID.length()-1);
     if (JFID==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }
    //QMessageBox::warning(this,"警告","继续保存！");
    //先查找一下是否存在这个科目代码
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT max(JF_id) as t  FROM JF_set WHERE (JF_id like '%1_' or JF_id='%1') and type_id='%2' ").arg(JFID.trimmed()).arg(mySQL::TypeId);

    query.exec(SQL);
    while(query.next()){
        QString str=query.value("t").toString().trimmed();
        if (str==""){
            QMessageBox::warning(this,"警告","未发现项目的存在，请重新选择！");
            return;
        }
        if (str==JFID){
            //如果没有下级科目就添加第一个项目
            JFID+="0";
        }else{
            //有下级科目的情况下取出最大看值
            QString MaxKM=str.right(1).toUtf8();
            int K_ascii ;
            QVector<uint>v=MaxKM.toUcs4();
                foreach(int a,v)
                {
                    qDebug()<<"Ascii:"<<a;
                    K_ascii=a;
                }
                //自动编号的次序为： 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ 到Z后将无法编号
                if (K_ascii>=90){
                    QMessageBox::warning(this,"警告","编号已经到了Z了无法再继续了！");
                    return;
                }
                if (K_ascii<57){
                    K_ascii++;
                }else{
                    if (K_ascii==57){
                        K_ascii=65;
                    }else{
                        K_ascii++;
                    }
                }
                JFID+=(QChar)K_ascii;
        }
    }
    QString JF_ID=JFID;
    QString JF_NAME=ui->txtJFMC->text().trimmed();
    QString JF_KMDM=ui->txtKMDM->text().trimmed();
    QString JF_KMMC=ui->txtKMMC->text().trimmed();
    QString JF_XMBH=ui->txtXMBH->text().trimmed();
    QString JF_KTBH=ui->txtKTBH->text().trimmed();
    QString JF_KTMC=ui->txtKTMC->text().trimmed();
    QString JF_BZ=ui->txtBZ->text().trimmed();
    QString JF_FZR=ui->txtFZR->text().trimmed();

    bool Ssign=false;
    bool Tsign=false;
    if (JF_ID==""){
        QMessageBox::warning(this,"警告","项目代码为空！");
        return;
    }

    SQL=QString("SELECT idd FROM JF_set WHERE JF_id ='%1' and type_id='%2' ").arg(JF_ID.trimmed()).arg(mySQL::TypeId);
    query.exec(SQL);
    Ssign=false;
    while(query.next()){
        Ssign=true;
        Idd=query.value("idd").toString();
    }
    Tsign=true;
    if (Ssign==false && JF_ID.length()>1){
        //新建 并 科目代码长度小于1  就判断是否存在上级科目
        int kl=JF_ID.length()-1;
        SQL=QString("SELECT idd FROM JF_set WHERE  type_id='%2' and (JF_id ='%1' ").arg(JF_ID.left(kl).trimmed()).arg(mySQL::TypeId);
        if (kl>=2){
            //兼容2位科目代码
            kl--;
            SQL+=QString(" or JF_id='%1'").arg(JF_ID.left(kl));
        }
        SQL+= ")";
        query.exec(SQL);
        Tsign=false;
        while(query.next()){
            Tsign=true;
        }
    }
    if (Tsign==true){
        //开始准备保存科目代码
        SQL="";
        if (Ssign==false ){
            SQL="INSERT INTO  JF_set(type_id,JF_id,JF_name,kmdm,kmmc,xmbh,ktbh,ktmc,fzr,bz) VALUES (";
            SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_ID.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_NAME.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KMDM.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KMMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_XMBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KTBH.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_KTMC.trimmed().replace("'","''"));
            SQL+=QString("'%1',").arg(JF_FZR.trimmed().replace("'","''"));
            SQL+=QString("'%1')").arg(JF_BZ.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }else{
            SQL="update JF_set set ";
            SQL+=QString("type_id='%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
            SQL+=QString("JF_id='%1',").arg(JF_ID.trimmed().replace("'","''"));
            SQL+=QString("JF_name='%1',").arg(JF_NAME.trimmed().replace("'","''"));
            SQL+=QString("kmdm='%1',").arg(JF_KMDM.trimmed().replace("'","''"));
            SQL+=QString("kmmc='%1',").arg(JF_KMMC.trimmed().replace("'","''"));
            SQL+=QString("xmbh='%1',").arg(JF_XMBH.trimmed().replace("'","''"));
            SQL+=QString("ktbh='%1',").arg(JF_KTBH.trimmed().replace("'","''"));
            SQL+=QString("ktmc='%1',").arg(JF_KTMC.trimmed().replace("'","''"));
            SQL+=QString("fzr='%1',").arg(JF_FZR.trimmed().replace("'","''"));
            SQL+=QString("bz='%1' ").arg(JF_BZ.trimmed().replace("'","''"));
            SQL+=QString("where idd=%1").arg(Idd.trimmed().replace("'","''"));
            //QMessageBox::warning(this,"警告",SQL);
        }
        if (SQL!=""){
            query.exec(SQL);
            QMessageBox::information(this,"信息","保存成功！");
            JFSZRefTree();
        }

    }else{
        QMessageBox::warning(this,"警告","不存在上级项目代码 无法保存！！");
    }
}

void FormJF::on_btnJFSZRefresh_clicked()
{
    JFSZRefTree();
}

void FormJF::on_btnJFSZDel_clicked()
{
    QString SQL="";
    QString JFid="";
    QString JFMC="";
    bool delft=false;
    JFid=ui->txtJFID->text().trimmed();
    JFMC=ui->txtJFMC->text().trimmed();
    SQL="确认删除这条记录吗？ 删除后是无法恢复的！！！！";
    if (QMessageBox::information(NULL, "提醒", SQL,QMessageBox::Yes | QMessageBox::No, QMessageBox::No)==QMessageBox::No){
        return;
    }
    SQL="";
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT count(JF_id) as t  FROM JF_set WHERE JF_id like '%1%' and type_id='%2'  ").arg(JFid.trimmed()).arg(mySQL::TypeId);
    //QMessageBox::warning(this,"警告",SQL);
    query.exec(SQL);
    SQL="";
    while(query.next()){
        if (query.value("t").toString().trimmed()=="1"){

            delft=true;
        }else{
            QMessageBox::information(this,"提醒","存在下级科目，不得删除！！");
            return;
        }

    }
    if (delft==true){
        SQL=QString("delete  FROM JF_set WHERE JF_id = '%1' and type_id='%2' ").arg(JFid.trimmed()).arg(mySQL::TypeId);
        query.exec(SQL);
        JFSZRefTree();
        QMessageBox::information(this,"提醒","删除成功");

    }
}

void FormJF::on_btnJFSZClose_clicked()
{
    ui->groupJFHSXM->setVisible(false);
}
